{$R-}

(*
	bufferSPort(port number, oldBuffer,sizeOfBuffer) -- Free the old buffer at the address specified by
	oldBuffer (if non-zero), and allocate a new buffer of sizeOfBuffer (or revert to default buffer if
	zero). Return the address of the allocated buffer or zero if sizeOfBuffer is zero.

	By Harry Chesley.  DO NOT call the author!  Contact Apple Developer 
	Support on AppleLink "MacDTS" or on MCI "MacTech".

	ŠApple Computer, Inc. 1987
	All Rights Reserved.

	To compile and link this file using Macintosh Programmer's Workshop,

	pascal -w bufferSPort.p
	link -m ENTRYPOINT -o HyperTerm -rt XFCN=3 -sn Main=bufferSPort bufferSPort.p.o "{MPW}"Libraries:interface.o
	
*)

{$S bufferSPort } 	{ Segment name must be the same as the command name. }

unit DummyUnit;

interface

uses MemTypes, QuickDraw, OSIntf, ToolIntf, HyperXCmd;

procedure EntryPoint(paramPtr: XCmdPtr);
	
implementation

const

return = chr(13);
linefeed = chr(10);
bs = chr(8);

type

Str31 = String[31];

procedure bufferSPort(paramPtr: XCmdPtr); forward;

procedure EntryPoint(paramPtr: XCmdPtr);

	begin
		bufferSPort(paramPtr);
	end;

procedure bufferSPort(paramPtr: XCmdPtr);

	var portNumber: integer;
		oldBufferPtr: Ptr;
		newBufferPtr: Ptr;
		newBufferSize: longInt;
		inPort, outPort: integer;
		str: Str255;

	{$I XCmdGlue.inc}

	procedure Fail(errMsg: Str255); { set theResult and quit }
		begin
			paramPtr^.returnValue := PasToZero(errMsg);
			exit(bufferSPort);
		end;

	begin
		if paramPtr^.paramCount <> 3 then Fail('parameter count is not 3');

		ZeroToPas(paramPtr^.params[1]^,str);		{ First parameter is port number. }
		portNumber := StrToNum(str);
		if (portNumber < 1) or (portNumber > 2) then Fail('invalid port number');
		ZeroToPas(paramPtr^.params[2]^,str);		{ Second parameter is old buffer address. }
		oldBufferPtr := Ptr(StrToNum(str));
		ZeroToPas(paramPtr^.params[3]^,str);		{ Third parameter is new buffer size. }
		newBufferSize := StrToNum(str);

		if portNumber = 1 then inPort := -6
		else inPort := -8;

		if newBufferSize = 0 then newBufferPtr := nil
		else newBufferPtr := NewPtr(newBufferSize);
		if SerSetBuf(inPort,newBufferPtr,newBufferSize) <> noErr then
			begin
				DisposPtr(newBufferPtr);
				Fail('SetSetBuf failed');
			end;
		if oldBufferPtr <> NIL then DisposPtr(oldBufferPtr);
		str := LongToStr(ord4(newBufferPtr));
		paramPtr^.returnValue := PasToZero(str);
	end;
end.
